/* 
 * ---------------------------------------------------------------
 * This is accelerometer handler class for android that implements
 * Sensor event listener.
 * 2011, by Wu Mu Xian
 * ---------------------------------------------------------------
*/

package mygame.myandroidgameframework;

import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 

public class MyAndroidAccelerometerHandler implements SensorEventListener {
	
	// acceleration in x, y, z axis
	float accelX; 
    float accelY; 
    float accelZ; 
 
    // the accelerometer handler
    public MyAndroidAccelerometerHandler(Context context) { 
        SensorManager manager = (SensorManager) context 
                .getSystemService(Context.SENSOR_SERVICE ); 
        if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER ).size() != 0) { 
            Sensor accelerometer = manager.getSensorList( 
                    Sensor.TYPE_ACCELEROMETER ).get(0); 
            manager.registerListener(this, accelerometer, 
                    SensorManager.SENSOR_DELAY_GAME ); 
        } 
    }
    
    // called when the accuracy changed
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// do nothing
	}

	// called whtne the sensor changed
	public void onSensorChanged(SensorEvent event) {
		accelX = event.values[0]; 
        accelY = event.values[1]; 
        accelZ = event.values[2]; 
	}
	
	// get the acceleration in x-axis
	public float getAccelX() { 
        return accelX; 
    } 
 
	// get the acceleration in y-axis
    public float getAccelY() { 
        return accelY; 
    } 
 
    // get the acceleration in z-aixs
    public float getAccelZ() { 
        return accelZ; 
    }
}
